package com.example.capsule_kafka.kafka;

import com.example.capsule_kafka.bean.UserInfoBean;
import com.example.capsule_kafka.configuration.Constant;
import com.example.capsule_kafka.configuration.KafkaConstant;
import com.example.capsule_kafka.service.UserInfoService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/7 22:06
 */
@Component
@Slf4j
public class UserTypeConsumer {

    @Autowired
    UserInfoService userInfoService;


    @KafkaListener(topics = KafkaConstant.USER_TOPIC)
    public void userTypeOperation(String record) {

        UserInfoBean userInfoBean = new Gson().fromJson(record, UserInfoBean.class);
        if (userInfoBean == null) {
            return;
        }

        int type = userInfoBean.getType();
        if (type == Constant.SAVE_USER) {
        userInfoService.doSaveUser(userInfoBean);
        } else if (type == Constant.UPDATE_USER) {
            userInfoService.doUpdateUserInfo(userInfoBean);
        } else if (type == Constant.DELETE_USER) {

        }


    }


}
